package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.service.EmployeeService;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.RoleService;
import cn.wolfcode.utils.UserContext;
import cn.wolfcode.vo.JsonResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工处理器
 *
 * @author 杨兴健
 * @Date 2020/7/8 9:55
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有员工
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {

        model.addAttribute("pageInfo", service.selectAllByQO(qo));
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("depts", departments);

        return "/employee/list";
    }

    /**
     * 根据id删除员工
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"employee:delete", "员工删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        service.delete(id);
        return new JsonResult(true, "删除成功！");
    }

    /**
     * 跳转到添加|更新页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions(value = {"employee:input", "员工更新|添加页面"}, logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model) {

        if (id != null) {
            Employee employee = service.selectById(id);
            model.addAttribute("employee", employee);
        }
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);

        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        return "/employee/input";

    }

    /**
     * 更新|添加处理
     *
     * @param employee
     * @param rids
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工更新|添加"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] rids) {

        service.saveOrUpdate(employee, rids);

        return new JsonResult(true, "操作成功！");
    }

    /**
     * 转跳到更新密码页面
     *
     * @return
     */
    @RequestMapping("/toUpdatePwd")
    public String toUpdatePwd() {
        return "/employee/updatePwd";
    }

    /**
     * 更新密码
     *
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword) {

        service.updatePwd(oldPassword, newPassword);

        //销毁会话，跳转到登录页面
        UserContext.getSession().invalidate();
        return new JsonResult(true, "修改密码成功！");
    }

    /**
     * 转跳到重置密码页面
     *
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/toResetPwd")
    public String toResetPwd(@ModelAttribute("id") Long id, @ModelAttribute("name") String name) {
        return "/employee/resetPwd";
    }

    /**
     * 重置密码
     *
     * @param id
     * @param newPassword
     * @return
     */
    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(Long id, String newPassword) {

        service.resetPwd(id, newPassword);
        return new JsonResult(true, "更新成功！");
    }

    /**
     * 更改状态
     *
     * @return
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Long id) {
        service.changeStatus(id);
        return new JsonResult(true, "操作成功");
    }

    /**
     * 检查用户名是否存在
     *
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(Long id, String name) {

        boolean flag = service.checkName(id, name);
        Map map = new HashMap();
        map.put("valid", flag);
        return map;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        service.batchDelete(ids);
        return new JsonResult(true, "操作成功！");
    }

    /**
     * 导出excel表
     */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse res) throws IOException {
        //设置文件下载的响应头
        res.setHeader("Content-Disposition", "attachment;filename=employee_import.xls");

        HSSFWorkbook sheets = service.exportXls();
        sheets.write(res.getOutputStream());

    }

    /**
     * 导入excel表的数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {

        service.importXls(file);
        return new JsonResult(true, "导入成功！");

    }
}
